using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Microsoft.WowAddonStudio.Lua.RuntimeService.WowBench;
using NUnit.Framework;

namespace Microsoft.WowAddonStudio.Lua.RuntimeService.Tests.WowBench
{
	[TestFixture]
	public class RichTextOutputControlTest
	{
		private RichTextBox txtOutput;
		private RichTextOutputControl outputControl;
		private const string LinkSeparator = "#";

		#region SetUp

		/// <summary>
		/// Sets up.
		/// </summary>
		[SetUp]
		public void SetUp()
		{
			if (txtOutput != null) txtOutput.Dispose();

			txtOutput = new RichTextBox
			            	{
			            		AcceptsTab = true,
			            		AutoWordSelection = true,
			            		BorderStyle = BorderStyle.None,
			            		Dock = DockStyle.Fill,
			            		Font = new Font("Courier New", 8.25F, FontStyle.Regular, GraphicsUnit.Point, ((0))),
			            		Name = "txtOutput",
			            		Size = new Size(682, 209),
			            		TabIndex = 2,
			            		Text = "",
			            		WordWrap = false
			            	};

			outputControl = new RichTextOutputControl(txtOutput);
		}

		#endregion

		#region TearDown

		/// <summary>
		/// Tears down.
		/// </summary>
		[TearDown]
		public void TearDown()
		{
			txtOutput.Dispose();
		}

		#endregion

		#region Tests

		[Test(Description = "")]
		[ExpectedException(typeof (ArgumentNullException))]
		public void Test_Creation()
		{
			outputControl = new RichTextOutputControl(null);
		}

		[Test(Description = "")]
		public void Test_SimpleAppend()
		{
			const string testText = "Test SimpleText.";
			int position = outputControl.AppendText(testText);
			Assert.AreEqual(testText.Length, position);
			Assert.AreEqual(testText, outputControl.Text);
			Assert.AreEqual(position, outputControl.Position);
		}

		[Test(Description = "")]
		public void Test_SimpleErrorAppend()
		{
			const string testText = "Test ErrorText.";
			int position = outputControl.AppendErrorText((testText));
			Assert.AreEqual(testText.Length + 1, position);
			Assert.AreEqual(String.Concat("\n", testText), outputControl.Text);
			Assert.AreEqual(position, outputControl.Position);
		}

		[Test(Description = "")]
		public void Test_SimpleInfoAppend()
		{
			const string testText = "Test InfoText.";
			int position = outputControl.AppendInfoText((testText));
			Assert.AreEqual(testText.Length + 1, position);
			Assert.AreEqual(String.Concat("\n", testText), outputControl.Text);
			Assert.AreEqual(position, outputControl.Position);
		}

		[Test(Description = "")]
		public void Test_ComplexAppend()
		{
			const string testText = "Test ComplexText.";
			int position = outputControl.AppendInfoText(testText);
			Assert.AreEqual(position, outputControl.Position);
			txtOutput.SelectionStart = 1;
			Assert.AreEqual(1, outputControl.Position);
			position = outputControl.AppendInfoText((testText));
			Assert.AreEqual(position, outputControl.Position);
			Assert.AreEqual(position, txtOutput.SelectionStart);
		}

		[Test(Description = "")]
		public void Test_ComplexTextWithLinkAppend()
		{
			const string testText = "Test ComplexText - #WoWBench;http://www.wowwiki.com/WoWBench/Getting_started#...";
			int position = outputControl.AppendText(testText);
			Assert.AreEqual(position, outputControl.Position);
			Console.WriteLine(outputControl.Text);
			Assert.IsFalse(outputControl.Text.Contains("#WoWBench;http://www.wowwiki.com/WoWBench/Getting_started#"));
		}

		[Test(Description = "")]
		public void Test_CleanLinkText()
		{
			const string linkText = "WoWBench#http://www.wowwiki.com/WoWBench/Getting_started";
			string result = linkText;
			if (linkText.Contains(LinkSeparator))
			{
				result = linkText.Substring(linkText.LastIndexOf(LinkSeparator) + 1,
											linkText.Length - linkText.LastIndexOf(LinkSeparator) - 1);
			}
			Console.WriteLine(result);
		}

		[Test(Description = "")]
		[Explicit]
		public void Test_LinkRegEx()
		{
			var linkRegEx = new Regex(@"([#]{1}[\w]+[;]{1}[\s\S]+[#]{1})");

			string input = "Starting #WoWBench;http://www.wowwiki.com/WoWBench/Getting_started#...";
			Assert.IsTrue(linkRegEx.Match(input).Success);

			input = "Starting WoWBench;http://www.wowwiki.com/WoWBench/Getting_started#...";
			Assert.IsFalse((linkRegEx.Match(input).Success));

			input = "Starting WoWBench;http://www.wowwiki.com/WoWBench/Getting_started...";
			Assert.IsFalse((linkRegEx.Match(input).Success));

			input = "Starting WoWBench;http://www.wowwiki.com/WoWBench/Getting_started...";
			Assert.IsFalse((linkRegEx.Match(input).Success));

			input = "Starting #;http://www.wowwiki.com/WoWBench/Getting_started#...";
			Assert.IsFalse((linkRegEx.Match(input).Success));

			input = "Starting #WoWBench;#...";
			Assert.IsFalse((linkRegEx.Match(input).Success));

			input = "Starting #WoWBe:nch;http://www.wowwiki.com/WoWBench/Getting_started#...";
			Assert.IsFalse((linkRegEx.Match(input).Success));


			linkRegEx = new Regex(@"#([\w]+[;]{1}[\s\S]+)#");
			input = "Starting #WoWBench;http://www.wowwiki.com/WoWBench/Getting_started#...";
			Match match = linkRegEx.Match(input);
			Assert.IsTrue(match.Groups.Count > 0);
			string linkText = match.Groups[0].Captures[0].Value;

			Console.WriteLine(linkText);
			Assert.IsNotNull(linkText);
			Assert.AreEqual(linkText, "#WoWBench;http://www.wowwiki.com/WoWBench/Getting_started#");


			string hyperlink = linkRegEx.Match(input).Groups[0].Captures[0].Value;
			hyperlink =
				hyperlink.Substring(hyperlink.LastIndexOf(";") + 1, hyperlink.LastIndexOf("#") - hyperlink.LastIndexOf(";") - 1).
					Trim();
			Console.WriteLine(hyperlink);

			hyperlink = linkRegEx.Match(input).Groups[0].Captures[0].Value;
			string linkName = hyperlink.Substring(hyperlink.IndexOf("#") + 1, hyperlink.LastIndexOf(";") - 1).Trim();
			Console.WriteLine(linkName);

			string text = input;
			string part1 = text.Substring(0, text.IndexOf("#") - 1);
			string part2 = text.Substring(text.LastIndexOf("#") + 1, text.Length - text.LastIndexOf("#") - 1);
			Console.WriteLine(String.Concat(part1, " ", linkName, part2));
		}

		#endregion
	}
}